package com.gxuwz.prrs.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxuwz.prrs.bean.CommonResponse;
import com.gxuwz.prrs.bean.Notice;
import com.gxuwz.prrs.bean.Page;
import com.gxuwz.prrs.config.OkHttpClientConfiguration;
import com.gxuwz.prrs.constant.HttpCodeConstants;
import com.gxuwz.prrs.enums.HttpMethod;
import okhttp3.*;

import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author 张钧
 * @Description
 * @create 2023-05-18 下午 03:30
 */
public class RequestUtils {

	public static final Gson GSON = new Gson();

	private static final OkHttpClient OK_HTTP_CLIENT = OkHttpClientConfiguration.getOkHttpClient();

	private static final String PREFIX = "http://10.0.2.2:4523/m1/2774526-0-default";

	private static <T> T request(String url, Type type, HttpMethod method,RequestBody requestBody) {
		Request.Builder requestBuilder = new Request.Builder()
				.url(PREFIX + url);
		switch (method){
			case GET:
				requestBuilder = requestBuilder.get();
				break;
			case POST:
				requestBuilder = requestBuilder.post(requestBody);
				break;
			case PUT:
				requestBuilder = requestBuilder.put(requestBody);
				break;
			case DELETE:
				requestBuilder = requestBuilder.delete();
				break;
			default:
				break;
		}
		Request request = requestBuilder.build();
		try (Response response = OK_HTTP_CLIENT.newCall(request).execute()) {
			if (response.isSuccessful()) {
				ResponseBody body = response.body();
				if (body != null) {
					return GSON.fromJson(body.string(), type);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static <T> CommonResponse<T> getRequest(String url,Type type){
		try {
			CommonResponse<T> result = CompletableFuture.supplyAsync(() -> RequestUtils.<CommonResponse<T>>request(url, type, HttpMethod.GET, null)).get();
			if(HttpCodeConstants.SUCCESS.equals(result.getCode())){
				return result;
			}
		} catch (ExecutionException | InterruptedException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	public static <T> CommonResponse<T> postRequest(String url,FormBody body,Type type){
		try {
			CommonResponse<T> s = CompletableFuture.supplyAsync(() -> RequestUtils.<CommonResponse<T>>request(url, type, HttpMethod.POST, body)).get();
			if(HttpCodeConstants.SUCCESS.equals(s.getCode())){
				return s;
			}
		} catch (ExecutionException | InterruptedException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	public static <T> T putRequest(String url,FormBody body,Type type){
		try {
			return  CompletableFuture.supplyAsync(() -> RequestUtils.<T>request(url, type, HttpMethod.PUT, body)).get();
		} catch (ExecutionException | InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T deleteRequest(String url){
		try {
			return  CompletableFuture.supplyAsync(() -> RequestUtils.<T>request(url, new TypeToken<T>() {
			}.getType(), HttpMethod.GET, null)).get();
		} catch (ExecutionException | InterruptedException e) {
			throw new RuntimeException(e);
		}
	}
}
